let panelIns = null
import { partServer } from 'anov-core'
/**
 * 获取vue实例
 * @param {Vue Instance} instance Vue实例
 */
function passPanelIns(instance) {
  panelIns = instance
}

/**
 * 关闭面板
 */
function close() {
  panelIns.close()
}
/**
 * 开启面板
 */
function open() {
  panelIns.open()
}
/**
 * 切换
 */
function toggle() {
  panelIns.toggle()
}
/**
 * 重置数据
 */
function reset() {
  panelIns.reset()
}
/**
 * 开启控制台
 */
function openConsole() {
  panelIns.openConsole()
}
/**
 * 关闭控制台
 */
function closeConsole() {
  panelIns.closeConsole()
}
/**
 * 切换控制台
 */
function toggleConsole() {
  panelIns.toggleConsole()
}
/**
 * 获取tabs
 */
function getTabs() {
  return partServer.getCompByTagName('a-tab-pane')
}

/**
 * 切换tab
 * @param {string} key 激活的key
 */
function activeTab(key) {
  panelIns.tabClick(key + '')
}
// 切换到下一个tab
function nextTab() {
  let currentActiveIndex = parseInt(panelIns.activeTab),
    tabLen = panelIns.getTabs().length
  currentActiveIndex++
  if (currentActiveIndex <= tabLen) {
    activeTab(currentActiveIndex)
  } else {
    activeTab('1')
  }
}
// 切换到上一个tab
function prevTab() {
  let currentActiveIndex = parseInt(panelIns.activeTab),
    tabLen = panelIns.getTabs().length
  currentActiveIndex--
  if (currentActiveIndex >= 1) {
    activeTab(currentActiveIndex)
  } else {
    activeTab(tabLen)
  }
}
/**
 * 设置禁用tab
 * @param {string array} tabKeys 要禁用的tab的key
 */
function disableTabs(tabKeys) {
  if (!tabKeys) return
  tabKeys = Array.isArray(tabKeys) ? tabKeys : [tabKeys]
  panelIns.setDisabledTabs(tabKeys)
}

function initBlock(block) {
  panelIns.setBlockDefaultOpen(block)
}
/**
 * 开启block
 * @param {string} ind 打开block
 */
function openBlock(index) {
  panelIns.openBlok(index)
}
/**
 * 折叠block
 * @param {string} ind block编号
 */
function collapseBlock(ind) {
  panelIns.collapseBlock(ind)
}
/**
 * 展开或折叠
 * @param {string} ind block编号
 */
function toggleBlock(ind) {
  panelIns.toggleBlock(ind)
}
/**
 * 控制是否只展开一个
 * @param {boolean} payload 是否只展开一个
 */
function uniqueOpenBlock(payload) {
  panelIns.setBlockUniqOpen(payload)
}
/**
 * 授权的数据
 * @param {array string} license 授权
 */
function _initLicense(license) {
  panelIns.setLicense(license)
}
/**
 * 初始化panel
 * @param {object} params 参数
 */
function init(params = {}) {
  let p = {
    activeTab: '1',
    disableTabs: [],
    defaultOpenBlock: [],
    uniqueOpened: false,
    permission: 'all'
  }
  Object.assign(p, params)
  activeTab(p.activeTab)
  disableTabs(p.disableTabs)
  uniqueOpenBlock(p.uniqueOpened)
  initBlock(p.defaultOpenBlock)

  _initLicense(p.permission)
}

export default {
  init,
  open,
  close,
  toggle,
  reset,
  openConsole,
  closeConsole,
  toggleConsole,
  getTabs,
  activeTab,
  nextTab,
  prevTab,
  disableTabs,
  openBlock,
  collapseBlock,
  toggleBlock,
  uniqueOpenBlock
}

export { passPanelIns }
